package com.fh.wanandroid.ui.navigation.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.fh.wanandroid.R;
import com.fh.wanandroid.ui.articledetail.ArticleDetailActivity;
import com.fh.wanandroid.ui.knowledge.KnowledgeDataList;
import com.fh.wanandroid.ui.navigation.bean.NavigationData;
import com.fh.wanandroid.utils.CommonUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NavigationRvAdapter extends RecyclerView.Adapter<NavigationRvAdapter.MyViewHolder> {

    Context context;

    List<NavigationData.DataBean> navigationDataList;


    //实现点击效果
    private ItemClickListener mItemClickListener;

    public NavigationRvAdapter(Context context, List<NavigationData.DataBean> navigationDataList) {

        this.context = context;

        this.navigationDataList = navigationDataList;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_navigation, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.itemNavigationTitle.setText(navigationDataList.get(position).getName());

        List<NavigationData.DataBean.ArticlesBean> articlesBeans = navigationDataList.get(position).getArticles();
        holder.itemNavigationFlowLayout.setAdapter(new TagAdapter<NavigationData.DataBean.ArticlesBean>(articlesBeans) {
            @Override
            public View getView(FlowLayout parent, int position, NavigationData.DataBean.ArticlesBean feedArticleData) {
                TextView tv = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.flow_layout_tv,
                        holder.itemNavigationFlowLayout, false);
                if (feedArticleData == null) {
                    return null;
                }
                String name = feedArticleData.getTitle();
                tv.setPadding(CommonUtils.dp2px(10), CommonUtils.dp2px(10),
                        CommonUtils.dp2px(10), CommonUtils.dp2px(10));
                tv.setText(name);
                tv.setTextColor(CommonUtils.randomColor());
                holder.itemNavigationFlowLayout.setOnTagClickListener((view, position1, parent1) -> {
//                    startNavigationPager(view, position1, parent, articlesBeans);

                    Intent intent = new Intent(context, ArticleDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", articlesBeans.get(position).getId());
                    bundle.putString("title", articlesBeans.get(position).getTitle());
                    bundle.putString("link", articlesBeans.get(position).getLink());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                    return true;
                });
                return tv;
            }
        });

//
//        holder.itemKnowledgeContent.setText(content);

//        setClickListener(holder, position);
    }

    private void setClickListener(MyViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null)
                    mItemClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {

        return navigationDataList.size();
    }

    public List<NavigationData.DataBean>  getmItems() {
        return navigationDataList;
    }

    public void setmItems(List<NavigationData.DataBean>  navigaitionDataList) {

        this.navigationDataList = navigaitionDataList;

        this.notifyDataSetChanged();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_navigation_title)
        TextView itemNavigationTitle;
        @BindView(R.id.item_navigation_flow_layout)
        TagFlowLayout itemNavigationFlowLayout;

        View itemView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    public ItemClickListener getmItemClickListener() {

        return mItemClickListener;
    }

    public void setmItemClickListener(ItemClickListener mItemClickListener) {

        this.mItemClickListener = mItemClickListener;
    }

    public interface ItemClickListener {
        void onClick(int position);
    }

}
